package com.quasar.hpatient.widget;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.vise.log.ViseLog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 自定义选择时间dialog
 * Created by Administrator on 2017/5/11 0011.
 */

public class dialog_my_date_code extends Dialog {

    public dialog_my_date_code(Context context) {
         this (context,R.style.quick_option_dialog);
    }


    TextView tv_starttime;
    TextView tv_accepttime;
    TextView tv_cancel;
    TextView tv_communication;


    public String frsttime;
    public String lasttime;

    @SuppressLint("InflateParams")
    public dialog_my_date_code(final Context context, int theme) {
        super(context, theme);
        View contentView = getLayoutInflater().inflate(
                R.layout.dialog_my_date_code, null);

        tv_starttime= (TextView) contentView.findViewById(R.id.tv_starttime);
        tv_accepttime= (TextView) contentView.findViewById(R.id.tv_accepttime);

        tv_cancel= (TextView) contentView.findViewById(R.id.tv_cancel);
        tv_communication= (TextView) contentView.findViewById(R.id.tv_communication);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //选择开始日期
        tv_starttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selest(tv_starttime,1);

                frsttime= (String) tv_starttime.getText();
            }
        });

        //选择接受日期
        tv_accepttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selest(tv_accepttime,0);
                lasttime= (String) tv_accepttime.getText();
            }
        });

        //取消
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_my_date_code.this.dismiss();
            }
        });

        //确定
        tv_communication.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //开始时间
                String statsTime=tv_starttime.getText().toString().trim();
                //结束时间
                String endTime=tv_accepttime.getText().toString().trim();
                if(statsTime==null || statsTime.length()==0){
                    LogAndToastUtil.toast(context,"请选择开始时间");
                    return;
                }

                if(endTime==null || endTime.length()==0){
                    LogAndToastUtil.toast(context,"请选择结束时间");
                    return;
                }

                ViseLog.d("开始"+statsTime+"+++"+endTime);
                listener.DialogDetermine(statsTime,endTime);
                dialog_my_date_code.this.dismiss();
            }
        });

        super.setContentView(contentView);

    }

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public void selest(final TextView textView,int stats){
        //选择手术日期   Date date = userInfo.getSurgeryday();
        Date date =null;
        if (date == null) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i2, int i3) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.YEAR, i);
                calendar.set(Calendar.MONTH, i2);
                calendar.set(Calendar.DAY_OF_MONTH, i3);
                textView.setText("" + format.format(calendar.getTime()));

            }
        }, year, month, day);

        //开始时间
        String statsTime=tv_starttime.getText().toString().trim();

        if(stats==1){
            DatePicker datePicker = datePickerDialog.getDatePicker();
            datePicker.setMaxDate(TimeZoneUtil.getStringToDate(format.format(new Date()), "yyyy-MM-dd"));
        }else{
            if(statsTime!=null && statsTime.length()>0){
                DatePicker datePicker = datePickerDialog.getDatePicker();
                datePicker.setMinDate(TimeZoneUtil.getStringToDate(statsTime, "yyyy-MM-dd"));
                datePicker.setMaxDate(TimeZoneUtil.getStringToDate(format.format(new Date()), "yyyy-MM-dd"));
            }
        }

        datePickerDialog.setTitle("请选择时间");
        datePickerDialog.show();
    }

    protected dialog_my_date_code(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public void setListener(OnCustomDialogDetermineListener listener) {
        this.listener = listener;
    }

    public OnCustomDialogDetermineListener listener;

    public interface OnCustomDialogDetermineListener{
        void DialogDetermine(String statsTime,String endTime);
    }

}
